from sqlalchemy import create_engine, Column, Integer, Float, String, DateTime, Boolean, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from datetime import datetime

Base = declarative_base()

class MarketData(Base):
    """市场数据表"""
    __tablename__ = 'market_data'
    
    id = Column(Integer, primary_key=True)
    exchange = Column(String(50), nullable=False)
    symbol = Column(String(20), nullable=False)
    timestamp = Column(DateTime, nullable=False)
    open = Column(Float)
    high = Column(Float)
    low = Column(Float)
    close = Column(Float)
    volume = Column(Float)
    created_at = Column(DateTime, default=datetime.utcnow)
    
    def __repr__(self):
        return f"<MarketData(exchange='{self.exchange}', symbol='{self.symbol}', timestamp='{self.timestamp}')>"

class OrderBook(Base):
    """订单簿数据表"""
    __tablename__ = 'orderbook'
    
    id = Column(Integer, primary_key=True)
    exchange = Column(String(50), nullable=False)
    symbol = Column(String(20), nullable=False)
    timestamp = Column(DateTime, nullable=False)
    side = Column(String(4), nullable=False)  # bid/ask
    price = Column(Float, nullable=False)
    amount = Column(Float, nullable=False)
    created_at = Column(DateTime, default=datetime.utcnow)
    
    def __repr__(self):
        return f"<OrderBook(exchange='{self.exchange}', symbol='{self.symbol}', side='{self.side}')>"

class Trade(Base):
    """交易记录表"""
    __tablename__ = 'trades'
    
    id = Column(Integer, primary_key=True)
    exchange = Column(String(50), nullable=False)
    symbol = Column(String(20), nullable=False)
    order_id = Column(String(50), nullable=False)
    trade_id = Column(String(50), unique=True)
    timestamp = Column(DateTime, nullable=False)
    side = Column(String(4), nullable=False)  # buy/sell
    type = Column(String(20), nullable=False)  # market/limit
    price = Column(Float, nullable=False)
    amount = Column(Float, nullable=False)
    fee = Column(Float)
    fee_currency = Column(String(10))
    status = Column(String(20), nullable=False)
    created_at = Column(DateTime, default=datetime.utcnow)
    
    def __repr__(self):
        return f"<Trade(exchange='{self.exchange}', symbol='{self.symbol}', order_id='{self.order_id}')>"

class Position(Base):
    """持仓记录表"""
    __tablename__ = 'positions'
    
    id = Column(Integer, primary_key=True)
    exchange = Column(String(50), nullable=False)
    symbol = Column(String(20), nullable=False)
    timestamp = Column(DateTime, nullable=False)
    side = Column(String(4), nullable=False)  # long/short
    entry_price = Column(Float, nullable=False)
    current_price = Column(Float, nullable=False)
    amount = Column(Float, nullable=False)
    leverage = Column(Float, default=1.0)
    unrealized_pnl = Column(Float)
    realized_pnl = Column(Float, default=0.0)
    status = Column(String(20), nullable=False)  # open/closed
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    def __repr__(self):
        return f"<Position(exchange='{self.exchange}', symbol='{self.symbol}', side='{self.side}')>"

class TradingSignal(Base):
    """交易信号表"""
    __tablename__ = 'trading_signals'
    
    id = Column(Integer, primary_key=True)
    exchange = Column(String(50), nullable=False)
    symbol = Column(String(20), nullable=False)
    timestamp = Column(DateTime, nullable=False)
    signal_type = Column(String(20), nullable=False)  # entry/exit
    side = Column(String(4), nullable=False)  # long/short
    price = Column(Float, nullable=False)
    strength = Column(Float)  # 信号强度 0-1
    executed = Column(Boolean, default=False)
    created_at = Column(DateTime, default=datetime.utcnow)
    
    def __repr__(self):
        return f"<TradingSignal(exchange='{self.exchange}', symbol='{self.symbol}', signal_type='{self.signal_type}')>"
